package ${actionPackage};
import com.qicai.common.page.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import AdminConstants;
import com.qicai.service.common.dto.AdminUserDTO;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import ${servicePackage}.${className}Service;
import ${topLevel}.dto.${className}DTO;
import static AdminConstants.SESSION_USER_KEY;
import com.qicai.service.common.util.SessionUtils;
/**
 *  ${tablecomment}控制器
 * @author   ${author}
 * @version ${classObjectName}Service.java, v 0.1  ${date?datetime}
 */
@Controller
@RequestMapping(value = "/admin/${classObjectName}", method = {RequestMethod.GET, RequestMethod.POST})
public class ${className}Controller {
	private static final Logger logger = LoggerFactory.getLogger(${className}Controller.class);
	/**${tablecomment}业务接口**/
        @Resource
        private ${className}Service ${classObjectName}Service;
        @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
        public String list(HttpServletRequest request, @RequestParam(value = "currentPage", required = false, defaultValue = "0") int currentPage,
                           Model view) {
            try {
                logger.debug("${classObjectName}.list start");
                ${className}DTO ${classObjectName} =new ${className}DTO();
                ${classObjectName}.setPage(currentPage);
            Page<${className}DTO> page = ${classObjectName}Service.query${className}Page(${classObjectName});
                logger.debug("${classObjectName}.list pageResult:{}", page.getResult());

                //放入page对象。
                view.addAttribute("page", page);
                logger.debug("${classObjectName}.list end");
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            return "${classObjectName}-list";
        }

        @RequestMapping(value = "/edit", method = {RequestMethod.PUT})
        public String edit(@RequestParam(value = "id", required = false, defaultValue = "0") Long id,
                           Model view) {
            logger.debug("${classObjectName}.edit start param.id={}", id);
            try {
                if (id != null && id > 0) {
                    ${className}DTO ${classObjectName} = ${classObjectName}Service.query${className}ById(id);
                    view.addAttribute("${classObjectName}", ${classObjectName});
                }
                logger.debug("${classObjectName}.edit end");
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            return "${classObjectName}-edit";
        }

        @RequestMapping(value = "/save", method = {RequestMethod.POST})
        @ResponseBody
        public String save(HttpServletRequest request, ${className}DTO ${classObjectName},
                           Model view) {
            logger.debug("${classObjectName}.save start param.${classObjectName}={}", ${classObjectName});
            try {
                AdminUserDTO sessionUser = SessionUtils.getSessionValue(request, SESSION_USER_KEY);
                if (${classObjectName} != null) {
                    ${classObjectName}.setCreatorId(sessionUser.getId());
                    long rows = ${classObjectName}Service.save${className}(${classObjectName});
                    logger.debug("${classObjectName}.save end");
                    if (rows > 0) {
                        return String.format(AdminConstants.WEB_IFRAME_SCRIPT, "保存成功！");
                    } else {
                        return String.format(AdminConstants.WEB_IFRAME_SCRIPT, "保存失败！");
                    }
                }

            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                return String.format(AdminConstants.WEB_IFRAME_SCRIPT, "保存失败！");
            }
            return String.format(AdminConstants.WEB_IFRAME_SCRIPT, "保存成功！");
        }

        @RequestMapping(value = "/delete", method = {RequestMethod.DELETE})
        @ResponseBody
        public String delete(@RequestParam(value = "id", required = false, defaultValue = "0") Long id,
                             Model view) {
            logger.debug("${classObjectName}.delete start param.id={}", id);
            try {
                long rows = ${classObjectName}Service.delete${className}(id);
                logger.debug("${classObjectName}.delete end");
                if (rows > 0) {
                    return String.format(AdminConstants.WEB_IFRAME_SCRIPT, "删除成功！");
                } else {
                    return String.format(AdminConstants.WEB_IFRAME_SCRIPT, "删除失败！");
                }
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                return String.format(AdminConstants.WEB_IFRAME_SCRIPT, "删除失败！");
            }
        }





}